package com.ruoyi.edh.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.EdhShoppingCart;
import com.ruoyi.edh.domain.vo.EdhShoppingCartVo;
import com.ruoyi.edh.mapper.EdhShoppingCartMapper;
import com.ruoyi.edh.service.IEdhShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 易订货-购物车Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhShoppingCartServiceImpl implements IEdhShoppingCartService
{
    @Autowired
    private EdhShoppingCartMapper edhShoppingCartMapper;

    /**
     * 查询易订货-购物车
     * 
     * @param id 易订货-购物车ID
     * @return 易订货-购物车
     */
    @Override
    public EdhShoppingCartVo selectEdhShoppingCartById(Long id)
    {
        return edhShoppingCartMapper.selectEdhShoppingCartById(id);
    }

    /**
     * 查询易订货-购物车列表
     * 
     * @param edhShoppingCart 易订货-购物车
     * @return 易订货-购物车
     */
    @Override
    public List<EdhShoppingCartVo> selectEdhShoppingCartList(EdhShoppingCart edhShoppingCart)
    {
        return edhShoppingCartMapper.selectEdhShoppingCartList(edhShoppingCart);
    }

    /**
     * 新增易订货-购物车
     * 
     * @param edhShoppingCart 易订货-购物车
     * @return 结果
     */
    @Override
    public int insertEdhShoppingCart(EdhShoppingCart edhShoppingCart)
    {
        List<EdhShoppingCartVo> edhShoppingCartVos = edhShoppingCartMapper.selectEdhShoppingCartList(edhShoppingCart);
        if (!edhShoppingCartVos.isEmpty()){
            EdhShoppingCartVo edhShoppingCartVo = edhShoppingCartVos.get(0);
            // 购物存在相同商品则进行数量增加
            EdhShoppingCart cart = new EdhShoppingCart();
            cart.setId(edhShoppingCartVo.getShoppingCartId());
            cart.setAmount(edhShoppingCart.getAmount() + edhShoppingCartVo.getAmount());
            return edhShoppingCartMapper.updateEdhShoppingCart(cart);
        }
        edhShoppingCart.setCreateTime(DateUtils.getNowDate());
        return edhShoppingCartMapper.insertEdhShoppingCart(edhShoppingCart);
    }

    /**
     * 设置为默认地址
     *
     * @param id
     */
    @Override
    public Integer defaultAdress(Long id) {
        Long userId = SecurityUtils.getUserId();
        edhShoppingCartMapper.setDefaultAdress(id);
        edhShoppingCartMapper.cancelDefaultAdress(id,userId);
        return 1;
    }

    /**
     * 购物城数据封装
     *
     * @param list
     */
    @Override
    public Map<String, Object> sortShoppingCart(List<EdhShoppingCartVo> list) {

        Map<String,Object> result = new HashMap<String,Object>(16);
        // 供货商
        Map<String,Object> supplierMap = new HashMap<String, Object>(16);
        // 供货商
        Set<String> suppliers = new TreeSet<String>();
        // 失效商品
        List<EdhShoppingCartVo> invaliGoods = new LinkedList<EdhShoppingCartVo>();
        // 有效商品
        List<EdhShoppingCartVo> effectiveGoods = new LinkedList<EdhShoppingCartVo>();

        list.forEach(edhShoppingCartVo -> {
            boolean a = 0 == edhShoppingCartVo.getStatus() ? effectiveGoods.add(edhShoppingCartVo) : invaliGoods.add(edhShoppingCartVo);
        });

        effectiveGoods.forEach(edhShoppingCartVo -> {
            if (suppliers.add(edhShoppingCartVo.getSupplierName())){
                supplierMap.put(edhShoppingCartVo.getSupplierName(),new LinkedList<EdhShoppingCartVo>());
            }
        });
        for (String supplier :suppliers){
           for (EdhShoppingCartVo effectiveGood :effectiveGoods){
               if (supplier.equals(effectiveGood.getSupplierName())){
                   ((LinkedList<EdhShoppingCartVo>)supplierMap.get(supplier)).add(effectiveGood);
               }
           }
        }

        result.put("effectiveGoods",supplierMap);
        result.put("effectiveGoodsCount",effectiveGoods.size());
        result.put("invaliGoods",invaliGoods);
        result.put("invaliGoodsCount",invaliGoods.size());

        return result;
    }

    /**
     * 修改易订货-购物车
     * 
     * @param edhShoppingCart 易订货-购物车
     * @return 结果
     */
    @Override
    public int updateEdhShoppingCart(EdhShoppingCart edhShoppingCart)
    {
        edhShoppingCart.setUpdateTime(DateUtils.getNowDate());
        return edhShoppingCartMapper.updateEdhShoppingCart(edhShoppingCart);
    }

    /**
     * 批量删除易订货-购物车
     * 
     * @param ids 需要删除的易订货-购物车ID
     * @return 结果
     */
    @Override
    public int deleteEdhShoppingCartByIds(String[] ids)
    {
        return edhShoppingCartMapper.deleteEdhShoppingCartByIds(ids);
    }

    /**
     * 删除易订货-购物车信息
     * 
     * @param id 易订货-购物车ID
     * @return 结果
     */
    @Override
    public int deleteEdhShoppingCartById(Long id)
    {
        return edhShoppingCartMapper.deleteEdhShoppingCartById(id);
    }
}
